package com.wobangkj.common.service.impl;

import com.github.pagehelper.PageHelper;
import com.wobangkj.common.dao.UserDao;
import com.wobangkj.common.model.crud.User;
import com.wobangkj.common.model.crud.UserDe;
import com.wobangkj.common.repository.UserRepository;
import com.wobangkj.common.service.UserService;
import com.wobangkj.tool.api.result.Result;
import com.wobangkj.tool.util.sql.CrudUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

import static com.wobangkj.tool.util.RestUtil.copyNonNullProperties;

@Service
@Slf4j
public class UserServiceImpl implements UserService {
    @Autowired
    private UserRepository repository;

    @Override
    public Object search(Map<String, Object> params, User data) {

        return CrudUtil.search(params, data, repository);
    }

    @Override
    public Object id(Long id) {
        return CrudUtil.getById(id, repository);
    }

    @Override
    public Object idDe(Long id) {
        List<UserDe> list = userDao.IdDe(id);

        if (list.size() == 0) {
            return Result.MapNoResult;
        }

        return Result.GetMapDataSuccess(list);
    }

    @Override
    public Object delete(Long id) {
        return CrudUtil.delete(id, repository);
    }

    @Autowired
    private UserDao userDao;

    @Override
    public Object searchDe(Map<String, Object> params, UserDe data) {

        // TODO mybatis 方式查询
        int clientPage = Integer.parseInt((String) params.get("clientPage"));
        int everyPage  = Integer.parseInt((String) params.get("everyPage"));

        com.github.pagehelper.Page<UserDe> page =  PageHelper.startPage(clientPage, everyPage);

        List<UserDe> list = userDao.searchDe(params);

        if (list.size() == 0) {
            return Result.MapNoResult;
        }

        return Result.GetMapDataPager(list, clientPage, (int) page.getTotal(), everyPage);
    }

    @Override
    public Object update(User user) {

        User existData = repository.findById(user.getId()).get();
        copyNonNullProperties(user, existData);
        return CrudUtil.update(existData, repository);
    }

    @Override
    @SuppressWarnings("Duplicates")
    public Object create(User user) {
        try {
            User createData = repository.save(user);
            // 使用 uuid 作为源 token
            //String     token = UUID.randomUUID().toString().replace("-", "");
            //TokenModel model = new TokenModel(createData.getId(), token);
            // 30 分钟有效期
            //cacheManager.set(model.getToken(), new CacheModel(TOKEN_MINUTE, model));
            return Result.GetMapData(Result.CodeCreate, Result.MsgCreate);//, model);
        } catch (Exception e) {
            return Result.GetMapData(Result.CodeSql, e.getCause().getCause().getMessage());
        }
    }
//	private CacheManager cacheManager;
//
//	@Autowired
//	public void setRedis(RedisConnectionFactory redisConnectionFactory) {
//		this.cacheManager = new RedisManager(redisConnectionFactory);
//	}

    @Override
    public Object getShopByName(String name, Integer clientPage, Integer everyPage) {
        Page<User> page = repository.findByName(name, PageRequest.of(clientPage-1, everyPage, Sort.by("id")));
        log.info("[jpa原生sql查询 shop列表:]" + page.getContent());
        return Result.GetMapDataSuccess(page.getContent());
    }

    @Override
    public List<User> findByNameLike(String name){return repository.findByNameLike(name);}

    @Override
    public User getByOpenid(String openid){return repository.getByOpenid(openid);}

    @Override
    public List<User> getskeyByOpenid(String openid){return repository.getskeyByOpenid(openid);}

    @Override
    public Long[] findMyJob(String id) {
        return repository.findMyJob(id);
    }

    @Override
    public Long[] findMyShare(String id) {
        return repository.findMyShare(id);
    }

    @Override
    public List<Map> jobIdList(Long id) {
        return repository.jobIdList(id);
    }

    @Override
    public List<Map> shareIdList(Long id) {
        return repository.shareIdList(id);
    }
}
